/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.tnmk.worktime.service;

import java.util.ArrayList;
import java.util.List;
import org.joda.time.DateTime;
import org.tnmk.worktime.service.dao.entry.SessionEntry;
import org.tnmk.worktime.utils.TimeUtils;
import org.tnmk.worktime.utils.TimePeriodUtils;

/**
 * @Deprecated Use WorkSessionDao instead
 * @author Administrator
 */
@Deprecated
public class HistoryService {

  private static final HistoryService INSTANCE = new HistoryService();

  public static HistoryService getInstance() {
    return INSTANCE;
  }

  /**
   * @param timeA
   * @param timeB
   * @return check timeA.endTime is not far from timeB.beginTime
   */
  private boolean isShortPeriod(DateTime a, DateTime b) {
    long timeA = a.getMillis();
    long timeB = b.getMillis();
    long distance = timeB - timeA;
    boolean rs = (distance < TimeUtils.ONE_HOUR_MILLIS * 2);
    return rs;
  }

  protected List<SessionEntry> loadPreviousSessionHistory(List<SessionEntry> history) {
    List<SessionEntry> previousSessionHistory = new ArrayList<SessionEntry>(history.size());
    previousSessionHistory.addAll(history);
    SessionEntry lastSession = null;
    if (previousSessionHistory.isEmpty()) {
      return previousSessionHistory;
    }
    lastSession = previousSessionHistory.get(previousSessionHistory.size() - 1);
    if (isShortPeriod(lastSession.getOutTime(), DateTime.now())) {
      previousSessionHistory.remove(previousSessionHistory.size() - 1);
    }
    return previousSessionHistory;
  }

  protected List<SessionEntry> loadThisWeekHistory(List<SessionEntry> history) {
    List<SessionEntry> weekHistory = new ArrayList<SessionEntry>();
    for (SessionEntry workTimeModel : history) {
      DateTime inTime = workTimeModel.getInTime();
      if (TimePeriodUtils.isInThisWeek(inTime)) {
        weekHistory.add(workTimeModel);
      }
    }
    return weekHistory;
  }

  protected List<SessionEntry> loadThisMonthHistory(List<SessionEntry> history) {
    List<SessionEntry> rs = new ArrayList<SessionEntry>();
    for (SessionEntry session : history) {
      DateTime inTime = session.getInTime();
      if (TimePeriodUtils.isInThisMonth(inTime)) {
        rs.add(session);
      }
    }
    return rs;
  }
}
